//
//  SettingTableViewCell.m
//  JunZeWristband
//
//  Created by Kendama on 2017/9/9.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "SettingTableViewCell.h"

NSString *const kSettingTableViewCellIdentifier = @"kSettingTableViewCellIdentifier";

@implementation SettingTableViewCell

+ (instancetype)dequeueReusableWithTableView:(UITableView *)tableView
{
    SettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kSettingTableViewCellIdentifier];
    if (cell == nil)
    {
        cell = [[SettingTableViewCell alloc]init];
    }
    return cell;
}

- (instancetype)init
{
    self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kSettingTableViewCellIdentifier];
    if (self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        [self masLayoutSubview];
    }
    return self;
}

- (void)didClickConnectButton
{
    self.switchButton.selected = !self.switchButton.selected;
    if (self.switchBlock) {
        self.switchBlock(self.switchButton.selected);
    }
}

- (void)masLayoutSubview
{
    __weak UIView *superView = self.contentView;
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(superView.mas_centerY);
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(54));
        make.right.mas_equalTo(self.switchButton.mas_left);
    }];
    
    [self.switchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(superView.mas_centerY);
        make.right.mas_equalTo(superView.mas_right).with.offset(px1080Width(-54));
    }];
}

- (UILabel *)nameLabel
{
    if(!_nameLabel)
    {
        UIView *superView = self.contentView;
        _nameLabel = [[UILabel alloc]init];
        [superView addSubview:_nameLabel];
        [_nameLabel setFont:[UIFont systemFontOfSize:15]];
        [_nameLabel setTextColor:[UIColor darkGrayColor]];
    }
    return _nameLabel;
}


- (UIButton *)switchButton
{
    if(!_switchButton)
    {
        UIView *superView = self.contentView;
        _switchButton = [[UIButton alloc]init];
        [superView addSubview:_switchButton];
        [_switchButton setImage:[UIImage imageNamed:@"but_off"] forState:UIControlStateNormal];
        [_switchButton setImage:[UIImage imageNamed:@"but_on"] forState:UIControlStateSelected];
        [_switchButton addTarget:self action:@selector(didClickConnectButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _switchButton;
}

@end
